from rest_framework.request import Request

from apps.myapp.filters import StockModelFilter, StockModelInstFilter
from apps.myapp.models import StockModel, StockModelInst
from apps.myapp.serializers import StockModelSerializer, StockModelCreateUpdateSerializer, ExportStockModelSerializer, \
    StockModelInstSerializer, StockModelInstCreateUpdateSerializer
from apps.vadmin.op_drf.filters import DataLevelPermissionsFilter
from apps.vadmin.op_drf.response import SuccessResponse
from apps.vadmin.op_drf.viewsets import CustomModelViewSet
from apps.vadmin.permission.permissions import CommonPermission

class StockModelViewSet(CustomModelViewSet):
    """
    项目管理 的CRUD视图
    """
    queryset = StockModel.objects.all()
    serializer_class = StockModelSerializer  # 序列化器
    create_serializer_class = StockModelCreateUpdateSerializer  # 创建/更新时的列化器
    update_serializer_class = StockModelCreateUpdateSerializer  # 创建/更新时的列化器
    filter_class = StockModelFilter  # 过滤器
    extra_filter_backends = [DataLevelPermissionsFilter]  # 数据权限类，不需要可注释掉
    update_extra_permission_classes = (CommonPermission,)  # 判断用户是否有这条数据的权限
    destroy_extra_permission_classes = (CommonPermission,)  # 判断用户是否有这条数据的权限
    create_extra_permission_classes = (CommonPermission,)  # 判断用户是否有这条数据的权限
    search_fields = ('name',)  # 搜索
    ordering = ['id']  # 默认排序
    # 导出
    export_field_data = ['项目序号', '项目名称', '项目编码', '创建者', '修改者', '备注']  # 导出
    export_serializer_class = ExportStockModelSerializer  # 导出序列化器
    # 导入
    import_field_data = {'name': '项目名称', 'code': '项目编码', 'person': '项目负责人ID', 'dept': '部门ID'}
    import_serializer_class = ExportStockModelSerializer

    def get_model_name(self,request: Request, *args, **kwargs):
        return SuccessResponse(StockModel.objects.filter(state="1").values('id','name'))


class StockModelInstViewSet(CustomModelViewSet):
    """
    项目管理 的CRUD视图
    """
    queryset = StockModelInst.objects.all()
    serializer_class = StockModelInstSerializer  # 序列化器
    create_serializer_class = StockModelInstCreateUpdateSerializer  # 创建/更新时的列化器
    update_serializer_class = StockModelInstCreateUpdateSerializer  # 创建/更新时的列化器
    filter_class = StockModelInstFilter  # 过滤器
    extra_filter_backends = [DataLevelPermissionsFilter]  # 数据权限类，不需要可注释掉
    update_extra_permission_classes = (CommonPermission,)  # 判断用户是否有这条数据的权限
    destroy_extra_permission_classes = (CommonPermission,)  # 判断用户是否有这条数据的权限
    create_extra_permission_classes = (CommonPermission,)  # 判断用户是否有这条数据的权限
    search_fields = ('name',)  # 搜索
    ordering = ['id']  # 默认排序
    # 导出
    export_field_data = ['模板id', '项目名称','备注']  # 导出
    export_serializer_class = ExportStockModelSerializer  # 导出序列化器
    # 导入
    import_field_data = {'name': '项目名称', 'code': '项目编码', 'person': '项目负责人ID', 'dept': '部门ID'}
    import_serializer_class = ExportStockModelSerializer